﻿
using System;
using System.Windows;

namespace WCYCReader.Drawing
{
    public class BufferedImageImpl : BufferedImage
    {
        protected Graphics graphics;
        protected int height;
        private ImageImpl image;
        private int type;
        protected int width;

        public BufferedImageImpl(sbyte[] data)
        {
            Dimension maxDimension = new Dimension
            {
                width = -1,
                height = -1
            };
            this.image = new ImageImpl(data, maxDimension, 0);
            this.height = this.image.getHeight();
            this.width = this.image.getWidth();
        }

        public BufferedImageImpl(int width, int height)
        {
            this.width = width;
            this.height = height;
            this.type = 2;
        }

        public BufferedImageImpl(int width, int height, int type)
        {
            this.width = width;
            this.height = height;
            this.type = type;
        }

        public void disposeGraphics()
        {
            if (this.graphics != null)
            {
                ((GraphicsImpl)this.graphics).EndBatchDrawing();
            }
        }

        public Graphics getGraphics()
        {
            if (this.graphics == null)
            {
                this.graphics = new GraphicsImpl(this.width, this.height);
            }
            return this.graphics;
        }

        public int getHeight()
        {
            return this.height;
        }

        public int getType()
        {
            return this.type;
        }

        public virtual UIElement getUIElement(Rect clip)
        {
            UIElement frameBuffer = null;
            if (this.image != null)
            {
                return this.image.SilverlightImage;
            }
            if (this.graphics != null)
            {
                frameBuffer = ((GraphicsImpl)this.graphics).FrameBuffer;
            }
            return frameBuffer;
        }

        public int getWidth()
        {
            return this.width;
        }
    }
}

